package io.admin.common.utils;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    public StringUtils() {
    }

    public static List<String> splitStringToList(String str, String regex) throws Exception {
        List<String> list = new ArrayList();
        if (null != str && !str.equals("")) {
            String[] strList = str.split(regex);

            for(int i = 0; i < strList.length; ++i) {
                list.add(strList[i]);
            }
        }

        return list;
    }

    public static String str2EncodingString(String str, String encoding) throws Exception {
        try {
            return null == str ? "" : URLEncoder.encode(str, "UTF-8");
        } catch (Exception var3) {
            return "";
        }
    }

    public static String setListToString(List<String> list, String split) throws Exception {
        if (null != list && list.size() != 0) {
            if (null == split) {
                split = "，";
            }

            String str = "";
            int length = list.size();

            for(int i = 0; i < length; ++i) {
                if (i < length - 1) {
                    str = str + (String)list.get(i) + split;
                } else {
                    str = str + (String)list.get(i);
                }
            }

            return str;
        } else {
            return "";
        }
    }


    public static String fastReplace(String str, String target, String replacement) {
        int targetLength = target.length();
        if (targetLength == 0) {
            return str;
        } else {
            int idx2 = str.indexOf(target);
            if (idx2 < 0) {
                return str;
            } else {
                StringBuilder buffer = new StringBuilder(targetLength > replacement.length() ? str.length() : str.length() * 2);
                int idx1 = 0;

                do {
                    buffer.append(str, idx1, idx2);
                    buffer.append(replacement);
                    idx1 = idx2 + targetLength;
                    idx2 = str.indexOf(target, idx1);
                } while(idx2 > 0);

                buffer.append(str, idx1, str.length());
                return buffer.toString();
            }
        }
    }

    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }
}
